<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Cat;
use App\Art;
use DB;
class ArtController extends Controller
{
  /**
   * 添加文章时的栏目查询
   * @return page  文章添加页面
   */
  
  public function getAdd()
  {
    $catlis=new \App\Cat();
    $catlist = $catlis->getTree();

    return view('admin.art.artadd',['catlist'=>$catlist]);
  }

  /**
   * @param  post提交添加数据
   * @param  Request $req    [Request对象]
   * @return [script] 成功提示添加成功 失败提示添加失败
   */
  
  public function postAdd(Request $req)
  {     
        if($req->file('art_img') !== null){
          $path = '/images/'. date('Ymd/',time());
          is_dir($path);
          $name =  time().'.jpg';
          $req->file('art_img')->move(public_path().$path, $name);

          $row = [
            'art_title' => $req->art_title,
            'art_author'=> $req->art_author,
            'cat_id'=> $req->cat_id,
            'art_img'  => $path . $name,
            'art_intro'  => $req->art_intro,
            'art_pubtime' => time(),
          ];

        }else{
            $row = [
            'art_title' => $req->art_title,
            'art_author'=> $req->art_author,
            'cat_id'=> $req->cat_id,
            'art_intro'  => $req->art_intro,
            'art_pubtime' => time(),
             ];
       }
     
        $art_id=DB::table('arts')->insertGetId($row);
        if($req->content !== null){
        $arr=[
          'art_id' => $art_id,
          'art_content' => $req->art_content,
        ];
        $art=DB::table('art_exts')->insert($arr);
        }else{
          $arr=[
          'art_id' => $art_id,
        ];
        $art=DB::table('art_exts')->insert($arr);
        }
        if($art){
          return '<script language="javascript">
                    alert("添加成功");
                    location.replace(document.referrer);
                  </script>';
        }else{
          return '<script language="javascript">
                    alert("添加失败");
                    location.replace(document.referrer);
                  </script>'; 
        }
  }

  /**
   * 查询所有文章
   * @param  Request $req    [Request对象]
   * @return array [返回一个数组到文章列表页]
   */
  
  public function getList(Request $req)
  {

    
    if($req->keyword == null){
       $artlist=DB::table('arts')->select('art_id','cat_id','art_title','art_author','art_pubtime','art_click')-> where('art_status',1)->paginate(6);
    }else{
      $artlist=DB::table('arts')->select('art_id','cat_id','art_title','art_author','art_pubtime','art_click')->where('art_title', 'like','%'.$req->keyword.'%')->where('art_status',1)->paginate(6);
    if($artlist==null){
        $artlist=DB::table('arts')->select('art_id','cat_id','art_title','art_author','art_pubtime','art_click')->where('art_author', 'like','%'.$req->keyword.'%')->where('art_status',1)->paginate(6);
        }
    }

    $catlist=DB::table('cats')->select('cat_id','cat_name','parentid')->where('cat_status',1)->get();
    $parentid=DB::table('cats')->select('cat_id','cat_name')->where('cat_status',1)->get();
    return view('admin.art.artlist',['artlist'=>$artlist,'catlist'=>$catlist,'parentid'=>$parentid]);
  }

  /**
   * 删除文章
   * @param  [int]  $art_id  [文章id]
   * @return [script] 成功提示删除成功 失败提示删除失败
   */
  
  public function getDel($art_id)
  {

      $art=Art::find($art_id);
      $art->art_status=0;
      $art->save();
      if($art){
         return '<script language="javascript">
                  alert("删除成功");
                  location.replace(document.referrer);
                </script>'; 
      }else{
         return '<script language="javascript">
                  alert("删除失败");
                  location.replace(document.referrer);
                </script>'; 
      }
  }
  /**
   * 修改文章时文章查询和栏目查询
   * @param  [int] $art_id [文章id]
   * @return page  文章修改页面
   */
  public function getEdit($art_id)
  {
    $artlist=DB::table('arts')->select('art_id','cat_id','art_title','art_author','art_img','art_intro')->where('art_id',$art_id)->where('art_status',1)->first();
    $art_exts=DB::table('art_exts')->select('art_content')->where('art_id',$art_id)->first();
    $catlist=Cat::select('cat_id','cat_name','parentid')->where('cat_status',1)->get();

    return view('admin.art.artedit',['a'=>$artlist,'ae'=>$art_exts,'catlist'=>$catlist]);
  }
  /**
   * post提交的修改数据
   * @param  Request $req    [Request对象]
   * @return [script] 成功提示修改成功 失败提示修改失败
   */
  public function postEdit(Request $req)
  {
    $art=Art::find($req->art_id);
    if($req->file('art_img') !== null){
      $path = '/images/'. date('Ymd/',time());
      is_dir($path) || mkdir($path, 0777, true);
      $name =  time().'.jpg';
      $req->file('art_img')->move(public_path().$path, $name);
      $art->art_img  = $path . $name;
    }
      $art->art_title = $req->art_title;
      $art->art_author= $req->art_author;
      $art->cat_id= $req->cat_id;
      $art->art_intro  = $req->art_intro;

    if($art->save()){
      $row=[
        'art_content' => $req->art_content,
      ];
      $art_exts=DB::table('art_exts')->where('art_id',$req->art_id)->update($row);
    if(!$art_exts){
      return '<script language="javascript">
                alert("修改成功");
                location.replace(document.referrer);
              </script>';
    
    }else{
      return '<script language="javascript">
                alert("修改失败");
                location.replace(document.referrer);
              </script>';
     }         
    }
  }
}
